// pages/carteFavorites/carteFavorites.js
import {
  baseUrl,
  ftp_share_image,
  get_carte_list,
  get_open_id,
  update_shield,
  update_stick,
  user_login
} from "../../utils/api.js"

const util = require('../../utils/util.js')
const app = getApp()
var haveTap = 1 // 1: 可以点击 0：不可点击
Page({
  /**
   * 页面的初始数据
   */
  data: {
    TaskShow: 0,
    phoneShow: 0,
    openid: '',
    phone: '',
    code: '',
    codePlace: '获取验证码',
    setInter: '',
    authSettingUser: 1,
    haveShare: 0, // 0：完成/未开始 1：分享
    SwitchTabList: [{
        id: 2,
        title: '收藏的',
      },
      {
        id: 3,
        title: '浏览过',
      },
      {
        id: 1,
        title: '他人推荐',
      }
    ],
    SwitchSelect: 2,
    DialogList: [{
        type: 0,
        // icon: '../../images/carteFavorites/share.png',
        title: ''
      },
      {
        type: 1,

        status: 0,
        icon: '../../images/ic_library_stick.png',
        title: '置顶'
      },
      {
        type: 2,
        status: 1,
        icon: '../../images/carteFavorites/stop_black.png',
        title: '屏蔽'
      }
    ],
    content: ''
  },
  // 点击进入个人名片页面
  bindMyCarte: function(e) {
    wx.navigateTo({
      url: '../myCarte/myCarte',
    })
  },
  //点击个人图标进入个人页面
  bindPersonTap: function(e) {
    const userInfo = app.globalData.userInfo
    const that = this
    // console.log(userInfo)
    if (that.data.CarteList.length != undefined) {
      for (var i = 0; i < this.data.CarteList.length; i++) {
        if (that.data.CarteList[i].is_allow != undefined) {
          if (that.data.CarteList[i].is_allow == 2) {
            if (!app.globalData.shareOptions.invite_id) {
              app.globalData.shareOptions.invite_id = that.data.CarteList[i].id;
              console.log('进入个人页面邀请人：', app.globalData.shareOptions.invite_id);
            }
          }
        }

      }
    }

    if (userInfo.type == 1) {
      app.globalData.SwitchTabParams = {
        v_id: app.globalData.shareOptions.invite_id
      }
    } else {
      app.globalData.SwitchTabParams = {
        v_id: userInfo.id
      }
    }
    wx.switchTab({
      url: '/pages/person/person',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.checkUserInfo()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    haveTap = 1
  },

  // 检测是否需要登录
  checkUserInfo: function() {
    const that = this
    let openid = wx.getStorageSync('openid')
    if (openid) {
      app.globalData.openid = openid
      that.setData({
        openid,
      })

      if (app.globalData.userInfo != undefined && app.globalData.userInfo.id > 0) {
        that.getCarteList(app.globalData.userInfo.id)

      } else {
        let url = '/user_api/getInfoByOpenID';
        app.HttpPostSend(
          url, {
            openid: openid
          },
          function(result_data) {
            if (result_data === '-10000') {
              that.setData({
                authSettingUser: 0
              });
            } else {
              if (result_data.msg != "success") {
                app.showTipMsg(result_data.msg);
              } else {
                if (!!result_data.data) { //如果后台有数据
                  app.globalData.userInfo = {
                    ...result_data.data
                  }
                  // console.log(app.globalData.userInfo);
                  wx.setStorage({
                    key: 'userInfo',
                    data: JSON.stringify({
                      ...result_data.data
                    })
                  })
                  haveTap = 1
                  that.setData({
                    userInfo: result_data.data
                  })
                  that.getCarteList(result_data.data.id)
                } else { //如果后台无数据
                  wx.login({
                    success(res) {
                      let params = {
                        code: res.code
                      }
                      app.HttpGetSend(get_open_id, params, function(result) {
                        if (result.msg != "success") {
                          app.showTipMsg(result.msg);
                        } else {
                          app.globalData.openid = result.data.openid.openid;
                          wx.setStorage({
                            key: 'openid',
                            data: result.data.openid.openid
                          })
                          that.setData({
                            openid: result.data.openid.openid
                          })
                          that.setData({
                            TaskShow: 1,
                            openid: result.data.openid.openid
                          })
                          that.getCarteList(result.data.userinfo.id)
                        }
                      }, 1)
                    }
                  })
                }
              }
            }

          })
      }

    } else {
      wx.login({
        success(res) {
          let params = {
            code: res.code
          }
          app.HttpGetSend(get_open_id, params, function(result) {
            if (result.msg != "success") {
              app.showTipMsg(result.msg);
            } else {
              app.globalData.openid = result.data.openid.openid;
              wx.setStorage({
                key: 'openid',
                data: result.data.openid.openid
              })
              that.setData({
                openid: result.data.openid.openid
              })
              that.setData({
                TaskShow: 1,
                openid: result.data.openid.openid
              })
              that.getCarteList(result.data.userinfo.id)
            }
          }, 1)
        }
      })
    }

    let systemInfo = wx.getSystemInfoSync()
    app.globalData.systemInfo = systemInfo
    this.setData({
      systemInfo
    })

  },

  // 获取用户信息
  bindGetUserInfo: function(e) {
    const that = this
    if (e.detail.errMsg == "getUserInfo:ok") {
      that.setData({
        authSettingUser: 1
      });
    }
    let userInfo = e.detail.userInfo
    app.HttpPostSend(
      user_login, {
        openid: that.data.openid,
        username: userInfo.nickName,
        // phone: that.data.phone,
        // code: that.data.code,
        logo: userInfo.avatarUrl,
        sex: userInfo.gender
      },
      function(result_data) {
        if (result_data === '-10000') {
          that.setData({
            authSettingUser: 0
          });
        } else {
          if (result_data.msg != "success") {
            app.showTipMsg(result_data.msg);
          } else {
            app.globalData.userInfo = {
              ...result_data.data
            }
            wx.setStorage({
              key: 'userInfo',
              data: JSON.stringify({
                ...result_data.data
              })
            })
            haveTap = 1
            that.setData({
              TaskShow: 0,
              // phoneShow: 0,
              userInfo: result_data.data
            })
            that.getCarteList(result_data.data.id, 1)
          }
        }

      })
  },

  // 获取名片列表
  getCarteList: function(uid, update_status) {
    const that = this
    if (update_status != undefined && update_status > 0) {
      that.getNewCarteList(uid);
    } else {
      wx.getStorage({
        key: 'CarteList_' + that.data.SwitchSelect,
        success(res) {
          var res_data = res.data;
          var CarteList = JSON.parse(res_data);
          // console.log(CarteList);
          wx.getStorage({
            key: 'DialogShow_' + that.data.SwitchSelect,
            success(re) {
              // console.log(JSON.parse(re.data));
              that.setData({
                CarteList: CarteList,
                DialogShow: JSON.parse(re.data)
              })
            },
            complete(re) {

            }
          })

        },
        fail(res) {
          that.getNewCarteList(uid);
        }
      })
    }

  },
  getNewCarteList: function(uid) {
    const that = this
    let params = {
      uid: uid,
      type: that.data.SwitchSelect
    }
    wx.showLoading({
      title: '数据加载中',
    })
    app.HttpGetSend(get_carte_list, params, function(res) {
      wx.hideLoading();
      if (res === '-10000') {
        that.setData({
          authSettingUser: 0
        });
      } else {
        if (res.msg != "success") {
          app.showTipMsg(res.msg);
        } else {
          wx.hideNavigationBarLoading(); //隐藏导航条加载动画。

          wx.stopPullDownRefresh(); //停止当前页面下拉刷新。
          let CarteList = new Array(res.data.length)
          let DialogShow = new Array(res.data.length)
          let checkNullEmpty = (aim) => {
            if (aim != '' && aim != null) {
              return true
            } else {
              return false
            }
          }
          if (res.data.length > 0) {
            // CarteList = [...res.data]
            for (let i = 0; i < CarteList.length; i++) {
              CarteList[i] = new Object()
              // 名片 id
              CarteList[i].id = res.data[i].id
              //是否是默认名片
              CarteList[i].is_allow = res.data[i].is_allow
              // 名片 屏蔽状态
              CarteList[i].is_shield = res.data[i].is_shield
              // 名片 置顶状态s_id
              CarteList[i].s_id = res.data[i].s_id
              // 名片 置顶状态
              CarteList[i].is_stick = res.data[i].is_stick
              // 名片 公司logo
              CarteList[i].comLogo = new Object()
              CarteList[i].comLogo.type = 0
              CarteList[i].comLogo.src = '../../images/carteFavorites/logo_block.png'
              if (checkNullEmpty(res.data[i].com_logo)) {
                CarteList[i].comLogo.type = 1
                CarteList[i].comLogo.src = res.data[i].com_logo
              }
              // 名片 公司名
              CarteList[i].comName = new Object()
              CarteList[i].comName.type = 0
              CarteList[i].comName.content = '公司'
              if (checkNullEmpty(res.data[i].company)) {
                CarteList[i].comName.type = 1
                CarteList[i].comName.content = res.data[i].company
              }
              // 名片 名字
              CarteList[i].name = new Object()
              CarteList[i].name.type = 0
              CarteList[i].name.content = '名字'
              if (checkNullEmpty(res.data[i].name)) {
                CarteList[i].name.type = 1
                CarteList[i].name.content = res.data[i].name
              }
              // 名片 职业
              CarteList[i].position = new Object()
              CarteList[i].position.type = 0
              CarteList[i].position.content = '职业'
              if (checkNullEmpty(res.data[i].position)) {
                CarteList[i].position.type = 1
                CarteList[i].position.content = res.data[i].position
              }
              // 名片 形象照
              CarteList[i].img = new Object()
              CarteList[i].img.type = 0
              CarteList[i].img.src = '../../images/img_block.png'
              if (checkNullEmpty(res.data[i].img)) {
                CarteList[i].img.type = 1
                CarteList[i].img.src = res.data[i].img
              }
              // 名片 分享图
              CarteList[i].share_img = new Object()
              CarteList[i].share_img.type = 0
              CarteList[i].share_img.src = '../../images/img_block.png'
              if (checkNullEmpty(res.data[i].share_img)) {
                CarteList[i].share_img.type = 1
                CarteList[i].share_img.src = res.data[i].share_img
              }
              // 名片 手机号
              CarteList[i].mobile = new Object()
              CarteList[i].mobile.type = 0
              CarteList[i].mobile.content = '手机号'
              if (checkNullEmpty(res.data[i].mobile)) {
                CarteList[i].mobile.type = 1
                CarteList[i].mobile.content = res.data[i].mobile
              }
              // 名片 邮箱
              CarteList[i].email = new Object()
              CarteList[i].email.type = 0
              CarteList[i].email.content = '邮箱'
              if (checkNullEmpty(res.data[i].email)) {
                CarteList[i].email.type = 1
                CarteList[i].email.content = res.data[i].email
              }
              // 名片 纪录时间
              switch (that.data.SwitchSelect) {
                case 2:
                  CarteList[i].show_time = util.formatTimeB(res.data[i].collection_time)
                  break;

                case 3:
                  CarteList[i].show_time = util.formatTimeB(res.data[i].browse_time)
                  break;

                case 1:
                  CarteList[i].show_time = util.formatTimeB(res.data[i].invite_time)
                  break;

                default:
                  break;
              }
              // 名片 右下角弹出层
              DialogShow[i] = new Object()
              DialogShow[i].showtype = 0
              DialogShow[i].DialogList = JSON.parse(JSON.stringify(that.data.DialogList))
              DialogShow[i].DialogList[1].status = res.data[i].is_stick
              if (res.data[i].is_stick == 1) {
                DialogShow[i].DialogList[1].title = '取消'
              }
              DialogShow[i].DialogList[2].status = res.data[i].is_shield
              if (res.data[i].is_shield == 2) {
                DialogShow[i].DialogList[2].title = '取消'
              }
            }
            // console.log(CarteList);

            that.setData({
              CarteList: CarteList,
              DialogShow: DialogShow
            })
          } else {
            that.setData({
              CarteList: CarteList,
              DialogShow: DialogShow
            })
          }
          wx.setStorage({
            key: "CarteList_" + that.data.SwitchSelect,
            data: JSON.stringify(CarteList)
          })

          wx.setStorage({
            key: "DialogShow_" + that.data.SwitchSelect,
            data: JSON.stringify(DialogShow)
          })
        }
      }

    })
  },

  // 头部 Tab 切换
  switchList: function(e) {
    // console.log(e)
    const that = this
    let tap_switch_id = e.detail.switch_id
    const SwitchSelect = that.data.SwitchSelect
    if (tap_switch_id == SwitchSelect) {
      console.log('has ture')
    } else {
      this.setData({
        SwitchSelect: tap_switch_id
      })
      this.getCarteList(app.globalData.userInfo.id)
    }
  },
  //滑动事件
  eventchange: function(e) {
    // console.log(event.detail.current)
    // this.setData({ current: event.detail.current })

    const that = this
    let tap_switch_id = e.detail.switch_id
    const SwitchSelect = that.data.SwitchSelect
    if (tap_switch_id == SwitchSelect) {
      console.log('has ture')
    } else {
      this.setData({
        SwitchSelect: tap_switch_id
      })
      this.getCarteList(app.globalData.userInfo.id)
    }
  },

  // 切换弹窗状态
  swicthDialog: function(e) {
    // if (this.data.SwitchSelect!=3){
    let aimDialog = 'DialogShow[' + e.currentTarget.dataset.idx + '].showtype'
    let aimDialogStatus = this.data.DialogShow[e.currentTarget.dataset.idx].showtype
    this.setData({
      [aimDialog]: aimDialogStatus == 0 ? 1 : 0
    })
    // }

  },
  swicthDialog1: function(e) {
    let aimDialog = 'DialogShow[' + e.currentTarget.dataset.idx + '].showtype'
    let aimDialogStatus = this.data.DialogShow.showtype
    this.setData({
      [aimDialog]: aimDialogStatus == 0 ? 1 : 0
    })
  },

  // 名片canvas渲染并打开分享遮罩层
  // canvasAndShowshare: function(e) {
  //   const that = this
  //   const idx = e.currentTarget.dataset.idx
  //   const CarteList = this.data.CarteList

  //   var url2 = "/cardshop_api/card_word";
  //   var params2 = {
  //     type: 3,
  //     uid: CarteList[idx].id
  //   }
  //   app.HttpPostSend(url2, params2,
  //     function (result_data) {
  //       if (result_data === '-10000') {
  //         that.setData({
  //           authSettingUser: 0
  //         });
  //       }else{
  //         if (result_data.msg != "success") {
  //           app.showTipMsg(result_data.msg);
  //         } else {
  //           if (!!result_data.data) {
  //             that.setData({
  //               content: result_data.data.content
  //             })
  //           }
  //         }
  //       }

  //     })
  //   const userInfo = this.data.userInfo
  //   this.setData({
  //     haveShare: 0,
  //   })
  //   // wx.showLoading({
  //   //   mask: 'true'
  //   // })
  //   this.setData({
  //     haveShare: 1,
  //     TaskShow: 2,
  //     shareInfo: {
  //       carte: CarteList[idx],
  //       share_id: userInfo.id,
  //       share_img: CarteList[idx].share_img.src
  //     }
  //   })
  //   // that.renderCanvasCarte(idx, CarteList[idx]).then(render_canvas_res => {
  //   //   // console.log(render_canvas_res)
  //   //   wx.hideLoading()
  //   //   that.setData({
  //   //     haveTap: 1,
  //   //     haveShare: 1,
  //   //     TaskShow: 2,
  //   //     shareInfo: {
  //   //       carte: CarteList[idx],
  //   //       share_id: userInfo.id,
  //   //       share_img: render_canvas_res
  //   //     }
  //   //   })
  //   // })
  // },

  // canvas渲染
  renderCanvasCarte: function(idx, params) {
    // console.log(idx, params.com_logo)
    const that = this
    // const params = this.data.CarteList[idx]
    const wxGetImageInfo = function(src) {
      let p = new Promise((resolve, reject) => {
        // console.log(server_url + uri)
        wx.getImageInfo({
          src: src,
          success(res) {
            resolve(res)
          },
          fail(err) {
            reject(err)
          },
          complete() {

          }
        })
      })
      return p
    }
    const weGetElementInfo = function(filterArr) {
      let p = new Promise((resolve, reject) => {
        // console.log(server_url + uri)
        const query = wx.createSelectorQuery()
        filterArr.map(items => {
          if (items.type == 1) {
            return query.select(items.content).boundingClientRect()
          } else if (items.type == 2) {
            return query.selectAll(items.content).boundingClientRect()
          }
        })
        query.exec(result => {
          resolve(result)
        })
      })
      return p
    }
    const toTempPath = new Promise((resolve, reject) => {
      Promise.all([
        weGetElementInfo([{
            content: '#carte_canvas_' + idx,
            type: 1
          },
          {
            content: '#cardBox_' + idx + ' .cardBox_bg',
            type: 1
          }, //背景图
          {
            content: '#cardBox_' + idx + ' .cardBoxTop_left .logo',
            type: 1
          }, //公司Logo
          {
            content: '#cardBox_' + idx + ' .cardBoxTop_left .company',
            type: 1
          }, //公司名字
          {
            content: '#cardBox_' + idx + ' .cardBoxTop_right .name',
            type: 1
          }, //名字
          {
            content: '#cardBox_' + idx + ' .cardBoxTop_right .job .job_text',
            type: 2
          }, //职位
          {
            content: '#cardBox_' + idx + ' .cardBoxTop_right .job .job_point',
            type: 1
          }, //职位
          {
            content: '#cardBox_' + idx + ' .cardBoxBottom_left .avatar_bg',
            type: 1
          }, //头像背景
          {
            content: '#cardBox_' + idx + ' .cardBoxBottom_left .avatar_content .avatar',
            type: 1
          }, //头像
          {
            content: '#cardBox_' + idx + ' .contactBox_phone .phone_icon',
            type: 1
          }, //电话
          {
            content: '#cardBox_' + idx + ' .contactBox_phone .phone_content',
            type: 1
          },
          {
            content: '#cardBox_' + idx + ' .contactBox_mail .mail_icon',
            type: 1
          }, //邮箱
          {
            content: '#cardBox_' + idx + ' .contactBox_mail .mail_content',
            type: 1
          },
        ]),
        wxGetImageInfo(params.comLogo.src),
        wxGetImageInfo(params.img.src),
      ]).then(res => {
        // console.log(res)
        let topBorder = res[0][0].top
        let leftBorder = res[0][0].left
        let canvasWidth = res[0][0].width
        let canvasHeight = res[0][0].height
        let getPosition = (type, index, idx) => {
          let aimTop = type == 3 ? res[0][index][idx].top - topBorder : res[0][index].top - topBorder
          let aimLeft = type == 3 ? res[0][index][idx].left - leftBorder : res[0][index].left - leftBorder
          if (type == 0 || type == 3) {
            return [aimLeft, aimTop]
          } else if (type == 1) {
            return [res[0][index].width, res[0][index].height]
          } else if (type == 2) {
            return [aimLeft, aimTop, res[0][index].width, res[0][index].height]
          }
        }
        const ctx = wx.createCanvasContext('carte_canvas_' + idx)
        // 背景图
        ctx.drawImage('https://mpian.oss-cn-hangzhou.aliyuncs.com/mingpian/20190522/0fa66799d95b7157d8145af56018daedc3413085.png', 0, 0, ...getPosition(1, 1))

        // 公司logo
        ctx.drawImage(res[1].path, ...getPosition(2, 2))

        // 公司名称
        ctx.setFontSize(11)
        ctx.fillStyle = '#333'
        ctx.setTextBaseline('top')
        ctx.fillText(params.comName.content, ...getPosition(0, 3))

        // 名字
        ctx.setFontSize(24)
        ctx.fillStyle = '#333';
        ctx.fillText(params.name.content, ...getPosition(0, 4))

        // 职位
        params.position.content.split('').map((items, idx) => {
          ctx.setFontSize(12)
          ctx.fillStyle = '#333'
          ctx.fillText(items, ...getPosition(3, 5, idx))
        })
        ctx.rect(...getPosition(2, 6))
        ctx.setFillStyle('#333')
        ctx.fill()

        // Logo背景图
        ctx.drawImage('../../images/carteFavorites/avatar_bg.png', ...getPosition(2, 7))

        // Logo
        ctx.drawImage(res[2].path, ...getPosition(2, 8))

        // 电话
        ctx.drawImage('../../images/carteFavorites/phone.png', ...getPosition(2, 9))
        ctx.setFontSize(10)
        ctx.fillStyle = '#fff'
        ctx.setTextBaseline('top')
        ctx.fillText(params.mobile.content, ...getPosition(0, 10))

        // 电话
        ctx.drawImage('../../images/carteFavorites/mail.png', ...getPosition(2, 11))
        ctx.setFontSize(10)
        ctx.fillStyle = '#fff'
        ctx.setTextBaseline('top')
        ctx.fillText(params.email.content, ...getPosition(0, 12))

        ctx.draw(false, function(canvas_res) {
          // console.log(canvas_res)
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 500,
            height: 400,
            destWidth: 500,
            destHeight: 400,
            canvasId: 'carte_canvas_' + idx,
            success(canvas_temp_res) {
              // resolve(canvas_temp_res.tempFilePath)
              // console.log(canvas_temp_res)
              wx.uploadFile({
                url: baseUrl + ftp_share_image, // 仅为示例，非真实的接口地址
                filePath: canvas_temp_res.tempFilePath,
                name: 'file',
                formData: {},
                success(upload_res) {
                  // console.log(upload_res)
                  let json_upload_res = JSON.parse(upload_res.data)
                  // resolve(upload_res.data)
                  // console.log(json_upload_res)
                  if (json_upload_res.msg != "success") {
                    app.showTipMsg(res.msg)
                    reject()
                  } else {
                    resolve(json_upload_res.data)
                  }
                },
                fail(upload_err) {
                  // console.log(upload_err)
                  reject(upload_err)
                }
              })
            },
            fail(canvas_temp_err) {
              reject(canvas_temp_err)
            }
          })
        })
      })
    })
    return toTempPath
  },

  // 关闭分享遮罩层
  HideShareMask: function() {
    this.setData({
      TaskShow: 0
    })
  },

  // 切换置顶状态
  switchStickStatus: function(e) {
    const that = this
    let getType = function(old_type) {
      // type：1：收藏 2：浏览 3：推荐
      if (old_type == 1) {
        // 推荐
        return 3
      } else if (old_type == 2) {
        // 收藏
        return 1
      } else if (old_type == 3) {
        // 浏览
        return 2
      }
    }
    app.HttpGetSend(
      update_stick, {
        id: e.currentTarget.dataset.id,
        is_stick: e.currentTarget.dataset.status,
        type: getType(that.data.SwitchSelect)
      },
      function(res) {
        if (res === '-10000') {
          that.setData({
            authSettingUser: 0
          });
        } else {
          if (res.msg != "success") {
            app.showTipMsg(res.msg);
          } else {
            that.getCarteList(app.globalData.userInfo.id, 1)

          }
        }


      })
  },

  // 切换屏蔽状态
  switchShieldStatus: function(e) {
    const that = this
    let getType = function(old_type) {
      // type：1：收藏 2：浏览 3：推荐
      if (old_type == 1) {
        // 推荐
        return 3
      } else if (old_type == 2) {
        // 收藏
        return 1
      } else if (old_type == 3) {
        // 浏览
        return 2
      }
    }
    app.HttpGetSend(
      update_shield, {
        id: e.currentTarget.dataset.id,
        is_shield: e.currentTarget.dataset.status,
        type: getType(that.data.SwitchSelect)
      },
      function(res) {
        if (res === '-10000') {
          that.setData({
            authSettingUser: 0
          });
        } else {
          if (res.msg != "success") {
            app.showTipMsg(res.msg);
          } else {
            that.getCarteList(app.globalData.userInfo.id, 1)
          }
        }

      })
  },

  // 进入名片详情页
  showCarte: function(e) {
    // console.log(e)
    const that = this

    const carte_id = e.detail.carte_id
    if (haveTap == 0) {
      console.log('屏蔽重复点击')
    } else {
      haveTap = 0
      app.globalData.SwitchTabParams = {
        v_id: carte_id
      }
      haveTap = 1;
      app.globalData.shareOptions.invite_id = e.detail.carte_id;
      console.log('进入名片详情页邀请人：', app.globalData.shareOptions.invite_id);

      wx.switchTab({
        url: '/pages/carteShow/carteShow'
      })
      // this.renderCanvasCarte(0, this.data.CarteList[0])
    }
  },

  showSharePost: function() {
    const that = this
    wx.navigateTo({
      url: '/pages/sharePost/sharePost'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.data.setInter);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {


    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画。
    this.getCarteList(app.globalData.userInfo.id, 1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    const that = this
    const shareInfo = this.data.shareInfo
    const carte = this.data.shareInfo.carte
    const timestamp = new Date()
    // console.log(carte)
    app.radarAdd(carte.id, 3)

    // 发送模板消息-作品被转发通知
    app.sendTemplateMessage(app.globalData.SwitchTabParams.v_id, 2, '/radarPages/radarCenter/radarCenter?coming_status=1', '您的店商系统被' + app.globalData.userInfo.username + '转发了', '', '', '')
    if (e.from === 'button') {
      // 来自页面内转发按钮
    }
    if (that.data.content.indexOf("**") != -1) {
      var sharecontent = that.data.content.replace('**', carte.name.content);
    } else {
      sharecontent = that.data.content;
    }

    return {
      title: sharecontent,
      path: '/pages/carteShow/carteShow?carte_id=' + carte.id + '&share_id=' + shareInfo.share_id,
      imageUrl: shareInfo.share_img,
      complete(res) {
        // console.log(res)
        haveTap = 1
        that.setData({
          TaskShow: 0
        })
      }
    }
  }
})